<template>
  <div class="login-page">
    <div class="back">
      <!-- .native绑定组件的原生事件，属于组件根元素 -->
      <!-- $router.back() 返回上一次访问路由，forward go -->

      <!--
        @click
        Vue ->
        @click.native 让自己或者别人写的组件 相当于一个DOM元素
       -->
      <geek-icon @click.native="goBack" name="esay-close"></geek-icon>
    </div>
    <h3 class="title">短信登录</h3>
    <!-- 表单 -->
    <van-form ref="form">
      <van-field name="mobile" :rules="rules.mobile"  v-model="form.mobile" placeholder="请输入手机号"></van-field>
      <van-field name="code" :rules="rules.code"  v-model="form.code" placeholder="请输入验证码">
        <template #button>
          <span class="send" @click='sendCode'>{{second>0?second:'发送验证码'}}</span>
        </template>
      </van-field>
    </van-form>
    <van-button @click="login">登录</van-button>
  </div>
</template>
<script>
import { getSMScode, userLogin } from '@/api/user'
import to from 'await-to-js'
export default {
  name: 'Login',
  methods: {
    goBack () {
      this.$router.go(-1)
    },
    async login () {
      // 1.校验手机号和验证码
      const [err] = await to(this.$refs.form.validate())
      if (err) return this.$toast.fail(err.message)

      // 2。登录请求
      const [err2, data] = await userLogin(this.form)
      if (err2) return this.$toast.fail(err)
      // 登录成功，跳转到之前的进来的页面，存储token
      console.log(data.data.refresh_token)
      this.$store.commit('user/setToken', data.data.token)
      this.$store.commit('user/setRefreshToken', data.data.refresh_token)
      const returnUrl = this.$route.query.returnUrl ? this.$route.query.returnUrl : '/home'
      this.$router.replace(returnUrl)
    },
    async sendCode () {
      if (this.second > 0) return
      // 1.校验手机
      const [err] = await to(this.$refs.form.validate('mobile'))
      if (err) return this.$toast.fail(err.message)

      // 2.进行网络请求 （获取手机验证码）
      const [err2] = await getSMScode(this.form.mobile)
      if (err2) return this.$toast.fail(err)
      // 3.让发送验证码按钮变成倒计时
      this.second = 60
      if (this.timer) clearInterval(this.timer)
      this.timer = setInterval(() => {
        this.second--
        if (this.second === 0) {
          clearInterval(this.timer)
        }
      }, 1000)
    }
  },
  beforeDestroy () {
    clearInterval(this.timer)
  },
  data () {
    return {
      timer: null,
      second: 0,
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请填写正确手机号' },
          {
            pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '请输入正确手机号'
          }
        ],
        code: [
          { required: true, message: '请填写正确验证码' },
          {
            pattern: /^\d{6}$/,
            message: '请输入正确验证码'
          }
        ]
      }
    }
  }
}
</script>
<style scoped lang="less">
.login-page {
  padding: 0 32px;
  .back {
    height: 60px;
    display: flex;
    align-items: center;
    .geek-icon {
      font-size: 20px;
      color: #ccc;
      position: relative;
      left: -15px;
    }
  }
  .title {
    font-size: 22px;
    line-height: 1;
    padding: 30px 0;
  }
  .van-cell {
    padding: 20px 0;
    &::after{
      left: 0;
      right: 0;
    }
  }
  .van-button {
    width: 100%;
    margin-top: 40px;
    height: 50px;
    color: #fff;
    font-size: 16px;
    border: none;
    background: linear-gradient(to right,#FF9999,#FFA179);
  }
}
  .send {
    font-size: 12px;
    color: #A5A6AB;
  }
</style>
